package com.yanqu.road.server.cmd.sacrifice;

import com.yanqu.road.entity.config.sacrifice.SacrificeDetail;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.sacrifice.SacrificeProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.config.SacrificeConfigMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.sacrifice.SacrificeMgr;
import com.yanqu.road.server.pb.SacrificePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_SACRIFICE_BOSS_INFO, desc = "获取祭祀boss信息")
public class SacrificeBossInfoCmd extends AbstractCommand {

	private SacrificeDetail sacrificeDetail;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(!SystemOpenMgr.systemOpen(player, eSystemId.Sacrifice.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		if(!SacrificeConfigMgr.isSacrificeSystemOpenTime()){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		if(!SacrificeConfigMgr.isInSacrificeAttackBossTime()){
			ret = GameErrorCode.E_SACRIFICE_NO_ATTACK_BOSS_TIME;
			return;
		}
		sacrificeDetail = SacrificeMgr.getNowSacrificeDetail();
	}

	@Override
	public void sendData(GamePlayer player) {
		SacrificeProto.SacrificeBossRespMsg.Builder respMsg = SacrificeProto.SacrificeBossRespMsg.newBuilder();
		respMsg.setRet(ret);
		if (0 == ret) {
			if(null != sacrificeDetail) {
				respMsg.setCurrentBlood(sacrificeDetail.getCurrentBlood().toString());
				respMsg.setTotalBlood(sacrificeDetail.getTotalBlood().toString());
				if(!StringUtils.isNullOrEmpty(sacrificeDetail.getKillBossNickName())) {
					respMsg.setUserData(SacrificePb.parseSacrificeKillBossUserInfoMsg(sacrificeDetail.getKillBossNickName(), sacrificeDetail.getKillBossDecoration()));
				}
				if(sacrificeDetail.getKillBossUserId() > 0){
					respMsg.setKillUserId(sacrificeDetail.getKillBossUserId());
					UserInfo killUserInfo = UserMgr.getUserInfo(sacrificeDetail.getKillBossUserId());
					UserBaseInfo killBaseInfo = UserMgr.parseUserBaseInfo(killUserInfo, GameServer.getInstance().getServerId());
					if(null != killBaseInfo) {
						PlayerProto.PlayerBaseTempMsg.Builder killUserMsg = PlayerBasePb.parsePlayerBaseTempMsg(killBaseInfo);
						respMsg.setKillUserInfo(killUserMsg);
					}
				}
			}
		}
		player.sendPacket(Protocol.U_SACRIFICE_BOSS_INFO, respMsg);
	}

}
